import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import {config} from '../../../shared/smartadmin.config';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入

declare var $: any;
@Component({
	selector: 'sa-rail-manage',
	templateUrl: './rail-manage.component.html',
})
export class RailManageComponent implements OnInit {
	tableData:any= []; //table数据
	totalCount:number= 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number= 10; //内置的分页每页条数
	searchKey:string = ''; //搜索keyword
	companyId:string= '';
	startTime:string = '';//开始时间
	endTime:string = '';//结束时间
	selectedCompanyId:string = '';
	selectedCompanyName:string = '';
	railName:string='';//围栏名称
	constructor(private router: Router,private jsonApiService: JsonApiService,private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
			if(queryParams.nowPage){
				this.curPage=queryParams.nowPage;
			}
		});
	}
	//刷新
	refresh(){
		this.load();
	}
	ngOnInit() {
		var storageRailName=localStorage.getItem('storageRailName');
		if(storageRailName){
			this.railName=storageRailName;
			localStorage.removeItem('storageRailName');
		}
		this.load();
		System.import('script-loader!select2/dist/js/select2.min.js').then(()=>{
			
		})
	}
	//查询
	table_search(){
		this.curPage=1;
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//初始化加载list数据
	load() {
		var postData={
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				companyId:this.selectedCompanyId,//公司id
				startTime:'',//开始时间
				endTime:'',//结束时间
				name:common.stringTrim(this.railName),//围栏名称
			}
		}
		let reqdata='gridPager='+JSON.stringify(postData);
		this.jsonApiService.http("mapmarker/markerpagelist.html", reqdata).subscribe(data => {
			if(data.isSuccess){
				var len=data.exhibitDatas.length
				for(var i=0;i<len;i++){
					data.exhibitDatas[i].companyEntity = data.exhibitDatas[i].companyEntity?data.exhibitDatas[i].companyEntity:{companyName:''};

					//转换时间格式
					if(data.exhibitDatas[i].stayTime){
						var d = Math.floor(data.exhibitDatas[i].stayTime/60/24); 
						var h = Math.floor(data.exhibitDatas[i].stayTime/60%24);
						var m = Math.floor(data.exhibitDatas[i].stayTime%60);
						if(d<1){
							if(h<1){
								data.exhibitDatas[i].stayTime = m+'分';
							}else{
								data.exhibitDatas[i].stayTime = h+'时'+m+'分';
							}
						   
						}else {
							data.exhibitDatas[i].stayTime = d+'天'+h+'时'+m+'分';
						}
						// var time = data.exhibitDatas[i].stayTime * 60;
						// data.exhibitDatas[i].stayTime = lostTimeStr(time);
					}
				}
				this.tableData = data.exhibitDatas;
				this.totalCount = data.recordCount;
			}
		})
	}
	//分页事件回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	//删除围栏
	delRail(row){
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(()=>{
			var reqdata = "id=" + row.id;
			this.jsonApiService.http('mapmarker/deletemarker.html', reqdata).subscribe(result => {
				this.jsonApiService.message(result.message);
				this.refresh();
			})
		})
	}
	//编辑围栏
	editRail(row){
		localStorage.setItem('railId',row.id);
		this.router.navigate(['/car/car-path'],{ queryParams: {nowPage:this.curPage}});
		setTimeout(function(){
			$('#left-panel').find('.top-menu-invisible.open').find('.UL').css('display','none');
			$('#left-panel').find('.top-menu-invisible.open').removeClass('open');
			$('#left-panel').find('.top-menu-invisible.active').addClass('open');
			$('#left-panel').find('.top-menu-invisible.active').find('.UL').css('display','block');
		},300)
	}
	addRill(){
		this.router.navigate(['/car/car-path'],{ queryParams: {nowPage:this.curPage}});
		setTimeout(function(){
			$('#left-panel').find('.top-menu-invisible.open').find('.UL').css('display','none');
			$('#left-panel').find('.top-menu-invisible.open').removeClass('open');
			$('#left-panel').find('.top-menu-invisible.active').addClass('open');
			$('#left-panel').find('.top-menu-invisible.active').find('.UL').css('display','block');
		},300)
	}	
	//公司选中回调
	companySelected(data){
		this.selectedCompanyId = data.companyId;
		this.selectedCompanyName = data.companyName;
	}
}
